package org.qiyu.live.im.core.server.handler.impl;

import com.alibaba.fastjson2.JSON;
import io.netty.channel.ChannelHandlerContext;
import org.apache.dubbo.common.utils.StringUtils;
import org.idea.qiyu.live.framework.redis.starter.keys.MsgProviderCacheKeyBuilder;
import org.qiyu.live.im.core.server.common.ChannelHandlerContextCache;
import org.qiyu.live.im.core.server.common.ImContextAttr;
import org.qiyu.live.im.core.server.common.ImContextUtils;
import org.qiyu.live.im.core.server.common.ImMsg;
import org.qiyu.live.im.core.server.handler.SimplyHandler;
import org.qiyu.live.im.dto.ImMsgBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 *
 * 登出消息的处理逻辑统一收拢到这个类中
 * 作者：無言
 * 时间：2024/12/2 21:34
 * 描述：永无BUG
 */
@Component
public class LogoutMsgHandler implements SimplyHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogoutMsgHandler.class);

    @Override
    public void handler(ChannelHandlerContext ctx, ImMsg imMsg) {
        Long userId = ImContextUtils.getUserId(ctx);
        if(userId == null) {
            LOGGER.error("attr is error, imMsg is {}", imMsg);
            throw new IllegalArgumentException("attr is error");
        }

        //理想情况下，用户正常下线，将保存的channelHandlerContext移除即可
        ChannelHandlerContextCache.remove(userId);
        ctx.close();


    }
}
